using System.Collections;
using UnityEngine;

//时间管理器
public class TimeManager : Singleton<TimeManager>
{

    [Header("时停")]
    private float defaultTimeScale = 1;//默认游戏时间缩放
    private float bulletTimeScale;
    private float timeRecoveryDuration;

    [Header("击退")]
    public float knokbackForce = 5f;//击退力

    public override void Awake()
    {
        base.Awake();
        Time.timeScale = defaultTimeScale;
    }

    /// <summary>
    /// 触发时停
    /// </summary>
    /// <param name="bulletTimeScale">时间缩放程度</param>
    /// <param name="timeRecoveryDuration">多久恢复到默认游戏时间</param>
    public void BulletTime(float bulletTimeScale, float timeRecoveryDuration)
    {
        this.bulletTimeScale = bulletTimeScale;
        this.timeRecoveryDuration = timeRecoveryDuration;
        Time.timeScale = bulletTimeScale;
        StartCoroutine(nameof(TimeRecoveryCoroutine));
    }

    // 恢复到默认时间缩放的协程
    IEnumerator TimeRecoveryCoroutine()
    {
        float ratio = 0f;
        while (ratio < 1f)
        {
            ratio += Time.unscaledDeltaTime / timeRecoveryDuration;
            // 插值时间缩放从减速时间缩放到默认时间缩放
            Time.timeScale = Mathf.Lerp(bulletTimeScale, defaultTimeScale, ratio);

            yield return null; // 等待下一帧继续执行
        }
    }
}
